package com.wf.day_0606.test2;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @author mpk
 * @since 2024/6/6 18:28:30
 */
public class BufferDemo {
    public static void main(String[] args) throws IOException {

        // 记录程序开始时间
        long startTime = System.nanoTime();

         test();     //247.6497 毫秒        字节缓冲流
        // test1();    //4932.0876 毫秒     字符缓冲流
        //test2();        // 652.9571 毫秒  字节流
        // 记录程序结束时间
        long endTime = System.nanoTime();
        // 计算程序运行时间（以毫秒为单位）
        long durationInNano = endTime - startTime;
        double durationInMilli = (double) durationInNano / 1_000_000; // 将纳秒转换为毫秒

        System.out.println("程序运行时间： " + durationInMilli + " 毫秒");


    }

    public static void test() throws IOException {
        try (BufferedInputStream bfi = new BufferedInputStream(Files.newInputStream(Paths.get("src/A.mp4")));

             BufferedOutputStream bfo = new BufferedOutputStream(Files.newOutputStream(Paths.get("src/B.mp4")));) {


            byte[] bytes = new byte[1024];

            while (bfi.read(bytes) != -1) {
                bfo.write(bytes);
            }

        }


    }

    public static void test1() throws IOException {
        try (

                BufferedReader br = new BufferedReader(new FileReader("src/A.mp4"));
                BufferedWriter bw = new BufferedWriter(new FileWriter("src/B.mp4"))


        ) {


            char[] bytes = new char[1024];

            while (br.read(bytes) != -1) {
                bw.write(bytes);
            }


        }


    }

    public static void test2() throws IOException {
        try (


                FileInputStream inputStream = new FileInputStream("src/A.mp4");

                FileOutputStream outputStream = new FileOutputStream("src/B.mp4");) {


            byte[] bytes = new byte[1024];

            while (inputStream.read(bytes) != -1) {
                outputStream.write(bytes);
            }


        }


    }


}
